﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Repository;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Entities.EntityCode;

namespace RooHui.Infrastructure.Entities.Entity
{
    public class Member : IAccessible<P_Member, Member>, ILogicEntity<P_Member, Member>
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string RealName { get; set; }
        public string Photo { get; set; }
        public string HandPhone { get; set; }
        public bool HandPhoneCheck { get; set; }
        public string IDCardNo { get; set; }
        public bool IDCardCheck { get; set; }
        public string Email { get; set; }
        public string Token { get; set; }
        public bool EmailCheck { get; set; }
        public MemberType MemberType { get; set; }
        public bool IsActive { get; set; }
        public DateTime CreateTime { get; set; }
        public CreateType CreateType { get; set; }
        public string CreateUser { get; set; }
        public Gender Gender { get; set; }
        public string Education { get; set; }
        public string MaritalStatus { get; set; }
        public string Address { get; set; }
        public string RegisterIP { get; set; }
        public ItemStatus Status { get; set; }
        public string Agent { get; set; }
        public AgentInfo AgentInfo { get; set; }
        public Account Account { get; set; }
        public User User { get; set; }

        public Member LoadUser(U_User u_user)
        {
            this.User = new User().Set(u_user);
            return this;
        }

        public Member LoadAccount(U_Account u_account)
        {
            this.Account = new Account().Set(u_account);
            return this;
        }

        public Member LoadAgentInfo(P_AgentInfo p_agentInfo)
        {
            this.AgentInfo = new AgentInfo().Set(p_agentInfo);
            return this;
        }

        protected override void Covariant(P_Member model)
        {
            if (model != null)
            {
                this.Address = model.Address;
                this.CreateTime = model.CreateTime;
                this.CreateType = (Enum.CreateType)model.CreateType;
                this.CreateUser = model.CreateUser;
                this.Education = model.Education;
                this.Email = model.Email;
                this.EmailCheck = model.EmailCheck;
                this.Gender = (Gender)model.Gender;
                this.HandPhone = model.HandPhone;
                this.HandPhoneCheck = model.HandPhoneCheck;
                this.IDCardCheck = model.IDCardCheck;
                this.IDCardNo = model.IDCardNo;
                this.IsActive = model.IsActive;
                this.MaritalStatus = model.MaritalStatus;
                this.MemberType = (MemberType)model.MemberType;
                this.Password = model.Password;
                this.Photo = model.Photo;
                this.RealName = model.RealName;
                this.RegisterIP = model.RegisterIP;
                this.Token = model.Token;
                this.UserName = model.UserName;
                this.Status = (ItemStatus)model.Status;
                this.Agent = model.Agent;
            }
        }

        protected override P_Member Inverter(Member model)
        {
            if (model != null)
            {
                return new P_Member()
                {
                    Address = model.Address,
                    CreateTime = model.CreateTime,
                    CreateType = (int)model.CreateType,
                    CreateUser = model.CreateUser,
                    Education = model.Education,
                    Email = model.Email,
                    EmailCheck = model.EmailCheck,
                    Gender = (int)model.Gender,
                    HandPhone = model.HandPhone,
                    HandPhoneCheck = model.HandPhoneCheck,
                    IDCardCheck = model.IDCardCheck,
                    IDCardNo = model.IDCardNo,
                    IsActive = model.IsActive,
                    Status =(int)model.Status,
                    MaritalStatus = model.MaritalStatus,
                    MemberType = (int)model.MemberType,
                    Password = model.Password,
                    Photo = model.Photo,
                    RealName = model.RealName,
                    RegisterIP = model.RegisterIP,
                    Token = model.Token,
                    UserName = model.UserName,
                    Agent = model.Agent
                };
            }
            return null;
        }

        public Member Set(P_Member dbModel)
        {
            Covariant(dbModel);
            return this;
        }

        /// <summary>
        /// 将启用缓存,通过username查找
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override Member Find(string key)
        {
            return new MemberCache().GetItem(key);
        }
        public override Member PostDelete()
        {
            new MemberCache().RefreshItem(UserName);
            return base.PostDelete();
        }
        public override Member PostModify()
        {
            new MemberCache().RefreshItem(UserName);
            return base.PostModify();
        }
    }
}
